package com.hy.frame.recycler;

import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class GridItemDecoration extends LinearItemDecoration {

    private int spanCount;

    public GridItemDecoration(RecyclerView rcyList, int divider) {
        this(rcyList, divider, 0);
    }

    public GridItemDecoration(RecyclerView rcyList, int divider, int color) {
        super(rcyList, divider, color);
        init();
    }

    public GridItemDecoration(RecyclerView rcyList, int divider, Drawable drawable) {
        super(rcyList, divider, drawable);
        init();
    }


    private void init() {
        if (rcyList != null && rcyList.getLayoutManager() != null && rcyList.getLayoutManager() instanceof GridLayoutManager) {
            spanCount = ((GridLayoutManager) rcyList.getLayoutManager()).getSpanCount();
        } else {
            spanCount = -1;
        }
    }

    @Override
    public GridItemDecoration build() {
        if (spanCount > 1) {
            if (orientation == LinearLayoutManager.VERTICAL && paddingLeft > 0 && rcyList.getPaddingLeft() == 0) {
                rcyList.setPadding(paddingLeft, rcyList.getPaddingTop(), rcyList.getPaddingTop(), rcyList.getPaddingBottom());
            } else if (orientation == LinearLayoutManager.HORIZONTAL && paddingTop > 0 && rcyList.getPaddingTop() == 0) {
                rcyList.setPadding(rcyList.getPaddingLeft(), paddingTop, rcyList.getPaddingTop(), rcyList.getPaddingBottom());
            }
        }
        return this;
    }

    @Override
    void drawDivider(Canvas canvas) {
        if (spanCount == 1) {
            super.drawDivider(canvas);
            return;
        }
        int left = 0;
        int top = 0;
        int right = 0;
        int bottom = 0;
        int vLeft = 0;
        int vTop = 0;
        int vRight = 0;
        int vBottom = 0;
        int childCount = rcyList.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = rcyList.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            int viewType = 0;
            if (adapter != null) {
                viewType = adapter.getItemViewType(params.getViewAdapterPosition());
            }
            if (viewType == BaseAdapter.TYPE_ITEM) {
                if (orientation == LinearLayoutManager.VERTICAL) {
                    left = child.getLeft();
                    top = child.getBottom();
                    right = child.getRight();
                    bottom = top + divider;
                    vLeft = child.getRight();
                    vTop = child.getTop();
                    vRight = vLeft + dividerVertical;
                    vBottom = child.getBottom() + divider;
                } else {
                    left = child.getLeft();
                    top = child.getBottom();
                    right = child.getRight();
                    bottom = top + divider;
                    vLeft = child.getRight();
                    vTop = child.getTop();
                    vRight = vLeft + dividerVertical;
                    vBottom = child.getBottom() + divider;
                }
                if (drawable != null) {
                    drawable.setBounds(left, top, right, bottom);
                    drawable.draw(canvas);
                    drawable.setBounds(vLeft, vTop, vRight, vBottom);
                    drawable.draw(canvas);
                } else if (mPaint != null) {
                    canvas.drawRect(left, top, right, bottom, mPaint);
                    canvas.drawRect(vLeft, vTop, vRight, vBottom, mPaint);
                }
            }
        }
    }
}
